home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / CGEMatrix.h next >
C/C++ Source or Header  |  1989-08-18  |  5KB  |  164 lines

  1. #ifndef CGEMATRIX_H
  2. #define CGEMATRIX_H
  3. #pragma once 
  4.  
  5. /*
  6.  *    Declarations for DComplex general matricies.
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)CGEMatrix.h    2.1    8/18/89
  27.  */
  28.  
  29. /*
  30.  *    This class is derived from class DComplexVec.  Data is stored
  31.  *    FORTRAN style: by columns.
  32.  *
  33.  *    Defining the preprocessor directive "BOUNDS_CHECK" will invoke
  34.  *    bounds checking.
  35.  */
  36.  
  37. #include "DComplexVec.h"
  38.  
  39. class CGEMatrix : public DComplexVec {
  40.   int ncols;            // Number of columns
  41.   int nrows;            // Number of rows
  42. protected:
  43.   void             assertColRange(int);
  44.   void             assertRowRange(int);
  45.   void            assertRowCol(const CGEMatrix&);
  46.   void            assertLength(const DComplexVec&);
  47.   void            assertSquare();
  48.   void            assertProduct(const CGEMatrix&);
  49.   void            assertProduct(const DComplexVec&);
  50. public:
  51.   CGEMatrix();
  52.   CGEMatrix(int rows, int cols);
  53.   CGEMatrix(int rows, int cols, DComplex initval);
  54.   CGEMatrix(const DComplex* dat, int, int);  // Copy of dat will be made
  55.   CGEMatrix(const DComplexVec& v, int, int); // Reference to v will be made
  56.   CGEMatrix(const CGEMatrix& m);       // Reference to m will be made
  57.  
  58.   DComplex*        data()        {return DComplexVec::data();}
  59.   int            cols();
  60.   int            rows();
  61.  
  62.   CGEMatrix&        reference(CGEMatrix& m); // Reference self to m
  63.   CGEMatrix        deepCopy();    // copy of self with distinct instance variables 
  64.   CGEMatrix        copy()        {return deepCopy();} // Synonym for deepCopy()
  65.   void            deepenShallowCopy();    // Guarantee that references==1:
  66.  
  67.   DComplexVec        operator[](int j);    // Return a col as a slice
  68.   DComplexVec        col(int j);        // Return a col as a slice
  69.   DComplexVec        row(int i);        // Return a row as a slice
  70.   DComplexVec        diagonal(int idiag=0);    // Return a diagonal as a slice
  71.   DComplex&        operator()(int i, int j); // Subscripting
  72.  
  73. // Math functions
  74.   CGEMatrix        product(const CGEMatrix&); // Inner product
  75.   DComplexVec        product(const DComplexVec&);
  76.  
  77. // Assignment operators --- self must be same size as m
  78.   CGEMatrix&        operator=(const CGEMatrix& m);
  79.   CGEMatrix&        operator=(DComplex);
  80.   CGEMatrix&        operator+=(const CGEMatrix& m);
  81.   CGEMatrix&        operator+=(DComplex);
  82.   CGEMatrix&        operator-=(const CGEMatrix& m);
  83.   CGEMatrix&        operator-=(DComplex);
  84.   CGEMatrix&        operator*=(const CGEMatrix& m);
  85.   CGEMatrix&        operator*=(DComplex);
  86.   CGEMatrix&        operator/=(const CGEMatrix& m);
  87.   CGEMatrix&        operator/=(DComplex);
  88.  
  89. // Increment/decrement operators
  90. //CGEMatrix&        operator++();
  91. //CGEMatrix&        operator--();
  92.  
  93. // Friendly arithmetic operators; Notice that operator* is an element-by-
  94. // element multiply, NOT a matrix multiply.
  95.   friend CGEMatrix    operator-(const CGEMatrix&);    // Unary minus
  96.   friend CGEMatrix    operator+(const CGEMatrix&);    // Unary plus
  97.   friend CGEMatrix    operator*(const CGEMatrix&, const CGEMatrix&);
  98.   friend CGEMatrix    operator/(const CGEMatrix&, const CGEMatrix&);
  99.   friend CGEMatrix    operator+(const CGEMatrix&, const CGEMatrix&);
  100.   friend CGEMatrix    operator-(const CGEMatrix&, const CGEMatrix&);
  101.   friend CGEMatrix    operator*(const CGEMatrix&, DComplex);
  102.   friend CGEMatrix    operator*(DComplex, const CGEMatrix&);
  103.   friend CGEMatrix    operator/(const CGEMatrix&, DComplex);
  104.   friend CGEMatrix    operator/(DComplex, const CGEMatrix&);
  105.   friend CGEMatrix    operator+(const CGEMatrix&, DComplex);
  106.   friend CGEMatrix    operator+(DComplex, const CGEMatrix&);
  107.   friend CGEMatrix    operator-(const CGEMatrix&, DComplex);
  108.   friend CGEMatrix    operator-(DComplex, const CGEMatrix&);
  109.  
  110. };
  111.  
  112. // Other (related) declarations:
  113. ostream&        operator<<(ostream& s, const CGEMatrix& m);
  114. CGEMatrix        transpose(const CGEMatrix&);
  115.  
  116. /******************* I N L I N E S **************************/
  117.  
  118. Inline int CGEMatrix::cols() { return ncols;}
  119. Inline int CGEMatrix::rows() { return nrows;}
  120. Inline void CGEMatrix::deepenShallowCopy(){DComplexVec::deepenShallowCopy();}
  121. Inline CGEMatrix operator+(const CGEMatrix& m)        { return m; }
  122. Inline CGEMatrix operator*(DComplex d, const CGEMatrix& m){ return m*d; }
  123. Inline CGEMatrix operator+(DComplex d, const CGEMatrix& m){ return m+d; }
  124.  
  125. // Return a column
  126. Inline DComplexVec CGEMatrix::operator[](int j){
  127. #if BOUNDS_CHECK
  128.   assertColRange(j);
  129. #endif
  130.   return DComplexVec::slice(j*nrows,nrows,1);
  131. }
  132.  
  133. Inline DComplexVec CGEMatrix::col(int j){    // Same as above
  134. #if BOUNDS_CHECK
  135.   assertColRange(j);
  136. #endif
  137.   return DComplexVec::slice(j*nrows,nrows,1);
  138. }
  139.  
  140. Inline DComplexVec CGEMatrix::row(int i){
  141. #if BOUNDS_CHECK
  142.   assertRowRange(i);
  143. #endif
  144.   return DComplexVec::slice(i, ncols, nrows);
  145. }
  146.  
  147. Inline DComplexVec CGEMatrix::diagonal(int i){
  148.   register int iabs=abs(i);
  149. #if BOUNDS_CHECK
  150.   assertSquare();
  151.   assertRowRange(iabs);
  152. #endif
  153.   return DComplexVec::slice(i>0 ? i*nrows : iabs, nrows-iabs, nrows+1);
  154. }
  155.  
  156. Inline DComplex& CGEMatrix::operator()(int i, int j){
  157. #if BOUNDS_CHECK
  158.   assertRowRange(i); assertColRange(j);
  159. #endif
  160.   return DComplexVec::operator()(j*nrows+i);
  161. }
  162.  
  163. #endif CGEMATRIX_HXX
  164.